package com.relax.mybatis.plugin.sensitive.strategy.impl;

import org.apache.commons.lang3.StringUtils;

import com.relax.mybatis.plugin.sensitive.strategy.SensitiveStrategy;

/**
 * @author relax
 * @date 2022/07/05
 * @desc 邮箱地址脱敏策略
 */
public class EmailSensitiveStrategy implements SensitiveStrategy {

    /**
     * <p>邮箱地址脱敏：显示前缀一到三位、@及后缀</p>
     *
     * <pre>
     * EmailSensitiveStrategy.handle("abc")                = "abc"
     * EmailSensitiveStrategy.handle("@189.cn")            = "@189.cn"
     * EmailSensitiveStrategy.handle("123@189.cn")         = "1****@189.cn"
     * EmailSensitiveStrategy.handle("12345@189.cn")       = "12***@189.cn"
     * EmailSensitiveStrategy.handle("123456789ab@189.cn") = "123********@189.cn"
     * </pre>
     *
     * @param data
     * @param symbol
     * @return
     */
    @Override
    public String handle(String data, String symbol) {
        int index = StringUtils.indexOf(data, "@");
        int leftSize = 1;
        if (index <= 1) {
            return data;
        } else if (index >= 4) {
            leftSize = 2;
        } else if (index >= 6) {
            leftSize = 3;
        }
        return StringUtils.rightPad(StringUtils.left(data, leftSize), index, symbol)
                .concat(StringUtils.mid(data, index, StringUtils.length(data)));
    }

}
